/*
 * @Author: your name
 * @Date: 2021-07-11 11:26:19
 * @LastEditTime: 2021-07-14 16:25:15
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \geek-dient-pc\src\router\index.js
 */
import VueRouter from 'vue-router'
import Vue from 'vue'

import Login from '@/views/login'
import Home from '@/views/home'
import Article from '@/views/article'
import Publish from '@/views/publish'
import Layout from '@/views/Layout'
import auth from '@/utils/auth'
// import {component} from 'vue/types/umd'
Vue.use(VueRouter)

const router = new VueRouter({
  routes: [
    { path: '/login', component: Login },
    {
      path: '/',
      component: Layout,
      children: [
        { path: '/', component: Home },
        { path: '/article', component: Article },
        { path: '/publish', component: Publish }
      ]
    }
  ]
})
// 导航守卫
router.beforeEach((to, from, next) => {
  const token = auth.getToken()
  if (!token && to.path !== '/login') {
    // 1. 如果未登录，访问的不是登录页面（访问后台页面），拦截到登录页面
    next('/login')
  } else {
    // 2.其他情况一概放行
    next()
  }
})

export default router
